home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-04-16 | 9.2 KB | 424 lines | [TEXT/CWIE] |
- /// DemoRouter.cpp
- // Copyright: © 1997 - 1998 by Apple Computer, Inc., all rights reserved.
-
- #include <stdio.h>
- #ifdef __mac_os
- #include <Files.h>
- #endif
-
- #include "IARouter.h"
-
- #include "SimpleAnalysis.h"
- #ifdef __mac_os
- #include "HFSCluster.h"
- #include "HFSStorage.h"
- #include "HFSIterator.h"
- #include "HFSCorpus.h"
- #include "HFSTextFolderCorpus.h"
- #else
- #include <sys/stat.h>
- #include <stdlib.h>
- #ifndef WIN32
- #include <limits.h>
- #include <unistd.h>
- #endif
-
- #include "UFSCluster.h"
- #include "UFSStorage.h"
- #include "UFSIterator.h"
- #include "UFSCorpus.h"
- #include "UFSDirectoryCorpus.h"
- #endif
- #include "VectorIndex.h"
-
-
-
- #ifdef DEBUG_NEW
- #include <DebugNew.h>
- #endif
-
- #if __profile__
- #include <profiler.h>
- #endif
-
- #ifdef __mac_os
- // enter the name of items to check for best router here
- StringPtr unSortedItems = "\pBike:Folders:Disorganized Press Releases";
- // enter name of the index here
- StringPtr singleIndexName = "\pBike:Folders:test.index";
-
- // enter the names of the router folders here
-
- StringPtr clusterFolders[] = {
- "\pBike:Folders:untitled folder",
- "\pBike:Folders:untitled folder 1",
- "\pBike:Folders:untitled folder 2",
- "\p" // empty string to mark end
- };
-
- long FSSpecToDirID( const FSSpec* inFSSpec );
- long FSSpecToDirID( const FSSpec* inFSSpec )
- {
- CInfoPBRec pb;
- OSErr err;
-
- pb.dirInfo.ioNamePtr = (StringPtr) inFSSpec->name;
- pb.dirInfo.ioVRefNum = inFSSpec->vRefNum;
- pb.dirInfo.ioFDirIndex = 0;
- pb.dirInfo.ioDrDirID = inFSSpec->parID;
- err = PBGetCatInfoSync(&pb);
- IAAssertion(err == noErr, "PBGetCatInfoSync() in FSSpecToDirID", IAAssertionFailure);
-
- return pb.dirInfo.ioDrDirID;
- }
-
- char* PToCStr(StringPtr source, char* dest);
- char* PToCStr(StringPtr source, char* dest)
- {
- int len = source[0];
- for (int i = 0; i < len; i++)
- dest[i] = source[i+1];
- dest[len] = 0;
-
- return dest;
- }
-
- #else
-
- typedef char* StringPtr;
-
- // enter the name of items to check for best router here
- StringPtr unSortedItems = "/Farzin/Folders/Disorganized";
- // enter name of the index here
- StringPtr singleIndexName = "/Farzin/Folders/test.index";
-
- // enter the names of the router folders here
-
- StringPtr clusterFolders[] = {
- "/Farzin/Folders/untitled",
- "/Farzin/Folders/untitled1",
- "/Farzin/Folders/untitled2",
- "" // empty string to mark end
- };
-
- #endif
-
-
- void AddItemsToIndex(StringPtr folderPathName, VectorIndex* inIndex);
- void AddItemsToIndex(StringPtr folderPathName, VectorIndex* inIndex)
- {
- #ifdef __mac_os
- FSSpec fsSpec;
- OSErr err = FSMakeFSSpec(0, 0, folderPathName, &fsSpec);
- IAAssertion(err == noErr, "Can't get folder", IAAssertionFailure);
-
- HFSIterator folderIterator(fsSpec.vRefNum, FSSpecToDirID(&fsSpec));
- IATry
- {
- while (folderIterator.Increment()) {
- CInfoPBRec* pb = folderIterator.GetPBRec();
-
- HFSDoc* doc = new HFSDoc((HFSCorpus*)inIndex->GetCorpus(), pb->hFileInfo.ioVRefNum, pb->hFileInfo.ioFlParID, pb->hFileInfo.ioNamePtr);
- inIndex->AddDoc(doc);
- }
- }
- IACatch (const IAException& exception)
- {
- printf("%s, %s\n", exception.What(), exception.GetLocation());
- }
- #else
- UFSIterator folderIterator((const byte*)folderPathName);
- byte name[PATH_MAX];
- IATry
- {
- while (folderIterator.Increment()) {
- name[0] = '\0';
- strcpy(name, folderIterator.GetPath());
- #ifndef WIN32
- strcat(name, "/");
- #else
- strcat(name, "\\");
- #endif
- strcat(name, folderIterator.GetFileName());
- UFSDoc* doc = new UFSDoc(name);
- inIndex->AddDoc(doc);
- }
- }
- IACatch (const IAException& exception) {
- printf("got exception!\n");
- }
- #endif
-
- }
-
- void DemoRouting();
- void DemoRouting()
- {
-
- #ifdef __mac_os
- FSSpec fsSpec;
- char str[256];
- // create/initialize our index
- (void)FSMakeFSSpec(0, 0, singleIndexName, &fsSpec);
-
- IAStorage* storage = MakeHFSStorage(fsSpec.vRefNum, fsSpec.parID, fsSpec.name);
- storage->Initialize();
-
- HFSCorpus* corpus = new HFSCorpus;
- IAAnalysis* an = new SimpleAnalysis();
-
- VectorIndex* index = new VectorIndex(storage, corpus, an);
- index->Initialize();
-
-
- // setup clusters
- uint32 clusterCount = 0;
- for (clusterCount = 0; clusterFolders[clusterCount][0] != 0; clusterCount++ ) {}
- HFSCluster** folders = new HFSCluster*[clusterCount];
-
- for (uint32 i = 0; i < clusterCount; i++ ) {
- folders[i] = new HFSCluster(index, clusterFolders[i]);
- }
-
- // instantiate a router and initialize with the corpuses representing
- // our clusters.
-
- IARouter myRouter (index);
- myRouter.InitializeClusters((IACluster**)folders, clusterCount);
-
-
- AddItemsToIndex(unSortedItems, index);
-
- index->Flush();
-
- HFSTextFolderCorpus* source = new HFSTextFolderCorpus(unSortedItems);
-
- IADocIterator* docs = source->GetDocIterator();
- IADoc* doc = docs->GetNextDoc();
- while (doc) {
- uint32 clusterIndex = myRouter.WhichCluster(doc, false);
- printf ("%s belongs in cluster %d\n", PToCStr(((HFSDoc*)doc)->GetFileName(), str), ++clusterIndex);
- delete doc;
- doc = docs->GetNextDoc();
- }
-
- delete docs;
- delete source;
-
- myRouter.Store();
- index->Flush();
- storage->Commit();
-
- delete index;
- delete storage;
-
-
- for (uint32 i = 0; i < clusterCount; i++ ) {
- delete folders[i];
- }
- delete [] folders;
-
- #else
-
- IAStorage* storage = MakeFileStorage(singleIndexName);
- storage->Initialize();
-
- UFSCorpus* corpus = new UFSCorpus;
- IAAnalysis* an = new SimpleAnalysis();
-
- VectorIndex* index = new VectorIndex(storage, corpus, an);
- index->Initialize();
-
-
- // setup clusters
- uint32 clusterCount = 0;
- for (clusterCount = 0; clusterFolders[clusterCount][0] != 0; clusterCount++ ) {}
- UFSCluster** folders = new UFSCluster*[clusterCount];
-
- for (uint32 i = 0; i < clusterCount; i++ ) {
- folders[i] = new UFSCluster(index, (const byte*)clusterFolders[i]);
- }
-
- // instantiate a router and initialize with the corpuses representing
- // our clusters.
-
- IARouter myRouter (index);
- myRouter.InitializeClusters((IACluster**)folders, clusterCount);
-
-
- AddItemsToIndex(unSortedItems, index);
-
- index->Flush();
-
- uint32 l;
-
- UFSIterator folderIterator((const byte*)unSortedItems);
- byte name[PATH_MAX];
-
- while (folderIterator.Increment()) {
- name[0] = '\0';
- strcpy(name, folderIterator.GetPath());
- #ifndef WIN32
- strcat(name, "/");
- #else
- strcat(name, "\\");
- #endif
- strcat(name, folderIterator.GetFileName());
- UFSDoc* doc = new UFSDoc(name);
- uint32 clusterIndex = myRouter.WhichCluster(doc, false);
- printf ("%s belongs in cluster %d\n", doc->GetName(&l), ++clusterIndex);
- delete doc;
- }
-
- myRouter.Store();
- index->Flush();
- storage->Commit();
-
- delete index;
- delete storage;
-
- for (uint32 i = 0; i < clusterCount; i++ ) {
- delete folders[i];
- }
- delete [] folders;
-
- #endif
- }
-
-
- void DemoRestore();
- void DemoRestore()
- {
-
- #ifdef __mac_os
- FSSpec fsSpec;
- char str[256];
- // create/initialize our index
- (void)FSMakeFSSpec(0, 0, singleIndexName, &fsSpec);
-
- IAStorage* storage = MakeHFSStorage(fsSpec.vRefNum, fsSpec.parID, fsSpec.name);
- storage->Open(true);
-
- HFSCorpus* corpus = new HFSCorpus();
- IAAnalysis* an = new SimpleAnalysis();
-
- VectorIndex* index = new VectorIndex(storage, corpus, an);
- index->Open();
-
-
- // instantiate a router and restore it
-
- IARouter myRouter (index);
- myRouter.Restore();
-
- IADocIterator* docs = NULL;
- IADoc* doc = NULL;
- HFSTextFolderCorpus* source = new HFSTextFolderCorpus(unSortedItems);
-
- docs = source->GetDocIterator();
- doc = docs->GetNextDoc();
- while (doc) {
- uint32 clusterIndex = myRouter.WhichCluster(doc, false);
- printf ("%s belongs in cluster %d\n", PToCStr(((HFSDoc*)doc)->GetFileName(), str), ++clusterIndex);
- delete doc;
- doc = docs->GetNextDoc();
- }
-
- delete docs;
- delete source;
- index->Flush();
- storage->Commit();
-
- delete index;
- delete storage;
-
- #else
-
- IAStorage* storage = MakeFileStorage(singleIndexName);
- storage->Open(true);
-
- UFSCorpus* corpus = new UFSCorpus;
- IAAnalysis* an = new SimpleAnalysis();
-
- VectorIndex* index = new VectorIndex(storage, corpus, an);
- index->Open();
-
- IARouter myRouter(index);
- myRouter.Restore();
-
- uint32 l;
-
- UFSIterator folderIterator((const byte*)unSortedItems);
- byte name[PATH_MAX];
-
- while (folderIterator.Increment()) {
- name[0] = '\0';
- strcpy(name, folderIterator.GetPath());
- #ifndef WIN32
- strcat(name, "/");
- #else
- strcat(name, "\\");
- #endif
- strcat(name, folderIterator.GetFileName());
- UFSDoc* doc = new UFSDoc(name);
- uint32 clusterIndex = myRouter.WhichCluster(doc, false);
- printf ("%s belongs in cluster %d\n", doc->GetName(&l), ++clusterIndex);
- delete doc;
- }
-
- index->Flush();
- storage->Commit();
- delete index;
- delete storage;
-
- #endif
-
- }
-
-
- void main() {
-
- #if __profile__
- #ifdef powerc
- ProfilerInit(collectDetailed, PPCTimeBase, 1500, 50);
- #else
- ProfilerInit(collectSummary, microsecondsTimeBase, 1500, 50);
- #endif
- #endif
-
- // make this bigger to use more memory and index faster
- IADiskBlockSize = 4096;
-
- IATry {
- DemoRouting();
- printf ("\n\n\n");
- DemoRestore();
- }
- IACatch (const IAException& exception) {
- #ifdef __mac_os
- printf("Caught exception: %s\n", exception.What());
- #else
- printf ("Exception thrown\n");
- #endif
- }
-
- IAReportMemoryUsage();
-
-
- #if __profile__
- ProfilerDump("\pDemoRouting.prof");
- ProfilerTerm();
- #endif
-
- #ifdef DEBUG_NEW
- DebugNewReportLeaks();
- #endif
- }
-
- #ifndef __mac_os
- #ifndef WIN32
- void terminate();
- void terminate() {exit(0);}
- #endif
- #endif
-